//if (window.addEventListener) {
//	  window.addEventListener("storage", handle_storage, false);
//	} else {
//	  window.attachEvent("onstorage", handle_storage);
//	};
	
//	alert("Tore : " + document.getElementById('main_board').rows[1].cells[1]);

//var gameShootInfoWhatIsGoingToReplayed='currentGameShootInfo';


var oldGameIndex= 0; 

function testing(){
	cellinfo= document.getElementById('main_board').rows[1].cells[1];
	//cellinfo= document.getElementById('side_board').rows[1].cells[1];
    var y = cellinfo.cellIndex;
    var x = cellinfo.parentNode.rowIndex;
    console.log("Cell: "+ y + " Row: " + x );
    markCell(cellinfo, 1);
}
//function testing2(){
//	//var names = [1, 2 ,3, 4 ];
//	//localStorage['names']=JSON.stringify(names);
//	var storedNames=JSON.parse(localStorage['names']);
//	console.log(storedNames);
//	
//}

//function testing3(){
//	
//	cellinfo= document.getElementById('side_board').rows[1].cells[1];
//    var y = cellinfo.cellIndex;
//    var x = cellinfo.parentNode.rowIndex;
//    console.log("Cell: "+ y + " Row: " + x );
//    markSbCell(cellinfo, 1);
//}

var saveShootingsList=[];
function saveShooting(cell, row, type, owner){
	// owner - 0 = you ; 1= opponent
	// type - reperesents cell type 1,2,3
	var shootinfo= [cell, row, type,owner];
	//localStorage['game1']=JSON.stringify(shootinfo);
	saveShootingsList.push(shootinfo);
	console.log(saveShootingsList);
	localStorage['currentGameShootInfo']=JSON.stringify(saveShootingsList);
	
}

function replayGame (){
		cleanTables();
		var i=0;
		var replayableShootingsList = JSON.parse(localStorage['LastGameShootInfo']);
		replayableShootingsList=replayableShootingsList[oldGameIndex];
		timer= setInterval(function() {
			if (i<replayableShootingsList.length){
			cell= replayableShootingsList[i][0];
			row= replayableShootingsList[i][1];
			if (replayableShootingsList[i][3] == 0){
				cellinfo= document.getElementById('main_board').rows[row].cells[cell];
				markCell(cellinfo, replayableShootingsList[i][2]);
			}else{
				cellinfo= document.getElementById('side_board').rows[row].cells[cell];
				markSbCell(cellinfo, replayableShootingsList[i][2]);
			}
			i++;
			
		}
			else{
				clearInterval(timer);
			}
		}, 1000);
		
	    
	};
	
	function replayGame2 (){
		cleanTables();
		var replayableShootingsList = JSON.parse(localStorage['LastGameShootInfo']);
		replayableShootingsList=replayableShootingsList[oldGameIndex];
		var ii=0;
		   $("#replay2").click(function(){
			   if (ii != 0){
				   ii--;
						cell= replayableShootingsList[ii][0];
						row= replayableShootingsList[ii][1];
					   if (replayableShootingsList[ii][3] == 0){
							deleteCellinfo= document.getElementById('main_board').rows[row].cells[cell];		
						}else{
							deleteCellinfo= document.getElementById('side_board').rows[row].cells[cell];		
						}
					   deleteCellinfo.innerHTML = "";
			   	
//			   	console.log(i);
			   }
		
			      }); 
		   
		   $("#replay3").click(function(){
			   if (ii<replayableShootingsList.length){
					cell= replayableShootingsList[ii][0];
					row= replayableShootingsList[ii][1];
					if (replayableShootingsList[ii][3] == 0){
						cellinfo= document.getElementById('main_board').rows[row].cells[cell];
						markCell(cellinfo, replayableShootingsList[ii][2]);
					}else{
						cellinfo= document.getElementById('side_board').rows[row].cells[cell];
						cellinfo.innerHTML = "";
						markSbCell(cellinfo, replayableShootingsList[ii][2]);
					}
						 
			//	   replayManually(ii);
				   ii++;
//				   console.log(i);
			   }
			      }); 
		   
//		   function replayManually(ii){
//   
//		
//		   }

	}



function cleanTables(){
	for (var i=1; i<11; i++){
		for (var j=1; j<11; j++){
	var square = document.getElementById("main_board").rows[i].cells[j];
    // remove ships from main board
    square.innerHTML = "";
    var square2 = document.getElementById("side_board").rows[i].cells[j];
    square2.innerHTML = "";
		}
	}
//	replayGame ();
}



////Create an object.
//var myObject = {};
// 
//// Store the object.
//localStorage.setItem( "myObject", myObject );
// 
//// Now, get the value and test the data type.
//console.log(
//"typeof( myObject ) ---",
//typeof( localStorage.getItem( "myObject" ) ));


function savePlayedGames () {
	if (localStorage['LastGameShootInfo'] != null){
		var temp = JSON.parse(localStorage['LastGameShootInfo']);
		temp.push(saveShootingsList);
		localStorage['LastGameShootInfo']=JSON.stringify(temp);
	}else{
		var shootList = [];
		shootList.push(saveShootingsList);
		localStorage['LastGameShootInfo']=JSON.stringify(shootList);
	}
	
}

function getOldGameListLength(){
	var temp = JSON.parse(localStorage['LastGameShootInfo']);
	return temp.length;
	}

